$(document).ready(function () {
    getLeaveWordList();
    initSubmit();
});
// 初始化留言内容列表
// 无论是否登录,游客或者会员都可以查看留言内容
function getLeaveWordList() {
    //其他初始化
    let obj = new Object();
    obj.m = "list";
    //初始化留言类型
    $.ajax({
        //请求方式 同步false 异步true
        async: true,
        //Ajax请求设置xhrField的withCredentials为true实现跨域访问
        xhrFields: {
            withCredentials: true
        },
        //请求方式
        type: "POST",
        //请求地址
        url: basePath + "/api/leaveWord",
        //返回值类型
        data: obj,
        dataType: "json",
        //成功之后的回调函数
        success: function (result) {
            console.log("get到来自后台的包含留言内容的result" + result);
            if (result.code == 200) {
                console.log("页面开始初始化！")
                initMessageView(result.data);
            }
        },
        //请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });
}

function initMessageView(result) {
    $(".leaveWordItem").html("");
    $.each(result, function (index, element) {
        //留言标题
        let div_leaveWordInfo_bg = $("<div></div>").addClass("leaveWordInfo bg");
        let div_title = $("<div></div>").addClass("title");
        let span_t1 = $("<span></span>").text("留言标题: ");
        let span_t2 = $("<span></span>").text(element.title);
        div_title.append(span_t1, span_t2);

        //留言人和留言时间
        let div_nameAndTime = $("<div></div>").addClass("nameAndTime");
        let span_n1 = $("<span></span>").text("顾客姓名: ");
        let span_n2 = $("<span></span>").text(element.memberName);
        let span_n3 = $("<span></span>").text("  留言时间: ");
        let span_n4 = $("<span></span>").text(element.leaveDate);
        div_nameAndTime.append(span_n1, span_n2, span_n3, span_n4);

        div_leaveWordInfo_bg.append(div_title, div_nameAndTime);

        let div_leaveWordContent = $("<div></div>").addClass("leaveWordContent").text(element.content);

        let div_replyAdmin_bg = null;
        let div_replyContent = null;
        if (element.answerContent != null) {
            div_replyAdmin_bg = $("<div></div>").addClass("replyAdmin bg").text("管理员回复: ");
            div_replyContent = $("<div></div>").addClass("replyContent").text(element.answerContent);
        }
        $(".leaveWordItem").append(div_leaveWordInfo_bg, div_leaveWordContent, div_replyAdmin_bg, div_replyContent);
    });
}

//初始化提交页面
function initSubmit() {
    $("#input_label").val("");
    $("#textarea_label").val("");
    // 获取提交按钮，将提交事件与之捆绑

    let publishSave = $("#submitLeaveWord");
    publishSave.bind("click", submitMessage);
}
function getStrActualLen(){
    var count=$("#textarea_label").val().length/1024;

    return Math.round(count*Math.pow(10,4));
}

function submitMessage() {
    let leaveWordTitle = $("#input_label").val();
    let leaveWordContent = $("#textarea_label").val();
    if (leaveWordTitle == "" || leaveWordContent == "") {
        alert("请您输入留言的标题、内容！");
    } else {
        if (getStrActualLen() >= 100) {
            alert("您输入的内容字数超过100字限制！");
        } else {
            let obj = new Object();
            obj.m = "submit";
            obj.leaveWordTitle = leaveWordTitle;
            obj.leaveWordContent = leaveWordContent;
            //ajax请求
            $.ajax({
                //请求方式 同步false 异步true
                async: true,

                xhrFields: {
                    withCredentials: true
                },
                //请求方式
                type: "POST",
                //请求地址
                url: basePath + "/api/leaveWord",
                //返回值类型
                data: obj,
                dataType: "json",
                //成功之后的回调函数
                success: function (result) {
                    console.log("留言提交成功！" + result);
                    getLeaveWordList();
                    $("#input_label").val("");
                    $("#textarea_label").val("");
                },
                //请求失败，包含具体的错误信息
                error: function (e) {
                    console.log(e.status);
                    console.log(e.responseText);
                    alert("请您登陆或注册后留言！")
                }
            });
        }
    }


}